//
//  HomeHotListenView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/20.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit

protocol HomeHotListenViewDelegate:NSObjectProtocol {
    func HomeHotListenViewClick(id:Int)
    func HomeHotListenHeadViewClick(id:Int)
}

class HomeHotListenView: UIView {
    
    weak var delegate:HomeHotListenViewDelegate?
    
    let HomeHotListenItemCellID = "HomeHotListenItemCell"
    let HomeListenTableViewHeaderViewID = "HomeListenTableViewHeaderView"
    
    var model: [ListenListModel]?
    var modelList: [ListenListModel]? {
        didSet {
            guard let model = modelList else { return }
            self.model = model
            self.tableView.reloadData()
        }
    }
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 390), style: UITableView.Style.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = WhiteBackColor
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        tableView.bounces = false
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(NIBNAME(HomeHotListenItemCellID), forCellReuseIdentifier: HomeHotListenItemCellID)
        tableView.register(HomeListenTableViewHeaderView.self, forHeaderFooterViewReuseIdentifier: HomeListenTableViewHeaderViewID)
        tableView.isScrollEnabled = false
        tableView.tableFooterView = UIView.init(frame: .zero)
        return tableView
    }()

    init(frame: CGRect, viewController:UIViewController) {
        super.init(frame: frame)
        self.addSubview(tableView)
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

//  MARK: - ###### 协议 ######
extension HomeHotListenView: UITableViewDelegate, UITableViewDataSource, HomeListenTableViewHeaderViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.model?.count == 1 ? 0:3
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 120
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: HomeHotListenItemCellID) as! HomeHotListenItemCell
        
        cell.leftImageView.sd_setImage(with: URL(string: StringIncludeChineseToString(self.model?[indexPath.row].shareImageUrl ?? "--")), completed: nil)
        cell.titleLabel.text = self.model?[indexPath.row].title ?? "--"
        cell.detailsLabel.text = self.model?[indexPath.row].describe ?? "--"
        cell.listenLabel.text = "\(String(describing: self.model?[indexPath.row].listenNum ?? 0))"
        cell.tolLabel.text = "\(String(describing: self.model?[indexPath.row].totalSeason ?? 0))集"
        cell.selectionStyle = .none
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let id:Int = self.model?[indexPath.row].id ?? 0
        delegate?.HomeHotListenViewClick(id: id)
        
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: HomeListenTableViewHeaderViewID) as! HomeListenTableViewHeaderView
        headerView.delegate = self
        
        return headerView
    }
    
    func homeListenTableViewHeaderViewAction() {
        delegate?.HomeHotListenHeadViewClick(id: 41)
    }
}
